<?php

App::uses('AppModel', 'Model');
App::uses('CompositeKeyModel', 'Model');

/**
 * MemberAccount Model
 *
 * @property Organisation $Organisation
 */
class MemberAccount extends CompositeKeyModel {

    //The Associations below have been created with all possible keys, those that are not needed can be removed

    public $useDbConfig = "organisations";

    /**
     * belongsTo associations
     *
     * @var array
     */
    public $belongsTo = array(
        'Member' => array(
            'className' => 'Member',
            'foreignKey' => 'member_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    public function attempt_login($username, $password) {
        $accounts = $this->find('all', array(
            'conditions' => array(
                'MemberAccount.username' => $username,
                'MemberAccount.active' => 1
            )
        ));

        foreach ($accounts as $account) {
            if ($account['MemberAccount']['password'] == md5($password . $account['MemberAccount']['pass_salt'])) {
                // todo other things
                return $account;
            }
        }

        return false;
    }

    public function getMemberAcounts($member_id) {
        return $this->find('all', array(
            'conditions' => array(
                'MemberAccount.member_id' => $member_id,
                'MemberAccount.active' => 1
            )
        ));
    }

}
